package com.lxy.equipment.listener;


import com.lxy.common.constant.QueueConstant;
import com.lxy.equipment.service.CompleteRepairService;
import com.lxy.equipment.service.RepairService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class RepairRequestsListener {
    @Autowired
    private RepairService repairService;
    @Autowired
    private CompleteRepairService completeRepairService;

    //异步通讯，生成维修单
    // TODO MQ发送Object较为复杂，暂定使用 id+remark 组成字符串发送再分割.
    @RabbitListener(queues = QueueConstant.REPAIR_QUEUE_NAME)
    public void createRepair(String repairMessage, Channel channel,@Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) throws IOException {
//        System.out.println(equipmentId);
        //手动发送ACK
        channel.basicAck(deliveryTag,false);
        repairService.createRepair(repairMessage);
    }

    //完成维修订单
    @RabbitListener(queues = QueueConstant.COMPLETE_REPAIR_QUEUE_NAME)
    public void CompleteRepair(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) throws IOException {
        channel.basicAck(deliveryTag,false);
        completeRepairService.CompleteRepair(message);

    }

}
